import java.util.Scanner;

public class GuessNumber {
    Scanner scan = new Scanner(System.in);
    int [] answer=new int[4];
    int [] guess=new int[4];
    public void generateAnswer() {
        int i = 0;
        while (i < 4) {
            int candidate = (int) (Math.random()*10);
            boolean hasDuplicate = false;
            for (int j = 0; j < i; j++) {
                if (answer[j] == candidate) {
                    hasDuplicate = true;
                    break;
                }
                if (hasDuplicate) {
                    continue;
                } else {
                    answer[i] = candidate;
                }
            }i = i + 1;
        }
    }
    public void receiveGuess(){
        int i=0;
        while (i < 4) {
            i++;
            try{
                guess[i]=scan.nextInt();
            }catch (Exception e ){
                System.out.println("请输入4个0-9之间不重复的数字!");

            }
        }
    }

    public void printGuess() {
        for (int i = 0; i < 4; i++) {
            System.out.println(guess[i]);

        }
    }
    public void printAnswer() {
        for (int i = 0; i < 4; i++) {
            System.out.println(answer[i]);

        }
    }
    public boolean hasDuplicateGuess(){
        for (int i = 0; i < 4; i++) {
            for (int j = i+1; j < 4; j++) {
                if (guess[i]==guess[j]){
                    System.out.println("有重复的数字！请重新输入！");
                    return true;
                }
            }
        }
        return false;
    }
    public int getA(){
        int a = 0;
        for (int i = 0; i < 4; i++) {
            if(answer[i]==guess[i]){
                a= a+1;
            }
        }
        return a;
    }
    public int getB(){
        int b = 4;
        for (int i = 0; i < 4; i++) {
            if(answer[i]==guess[i]){
                b= b-1;
            }
        }
        return b;
    }
    public void printNumbers(int t){
        int [] numbers;
        if (t==0){
            numbers=answer;
        }else {
            numbers = guess;
        }
        for (int i = 0; i <4 ; i++) {
            System.out.println(numbers[i]);
        }
    }
    public String playAgain(){
        return scan.next();
    }
    public static void main (String[]args){
        GuessNumber ngn = new GuessNumber();
        String again;
        do{
            ngn.generateAnswer();
            int a,b;
            do{
                System.out.println("请输入4个0-9之间不重复的数字");
                do{
                    ngn.receiveGuess();
                }while (ngn.hasDuplicateGuess());
                a = ngn.getA();
                b = ngn.getB();
                if(a!=4){
                    System.out.println(a+"A"+b+"B");
                }else{
                    System.out.println("恭喜你猜对啦！");
                }
            }while (a !=4);
            System.out.println("在玩一次吗？（y/n）");
            again = ngn.playAgain();
        }while(!again.equalsIgnoreCase("n"));

    }
}